﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using MVVMComun;
using Microsoft.Phone.Scheduler;

namespace CityWeather.ViewModel
{
    public class CityWeatherViewModel:NotificationObject
    {
       

        private ObservableCollection<Pronostico> pronosticos;

        public ObservableCollection<Pronostico> Pronosticos
        {
            get { return pronosticos; }
            set { pronosticos = value;
            OnPropertyChanged(this);
            }
        }
        



        private CityWeatherModel model = new CityWeatherModel();


        private string ciudad;

        public string Ciudad
        {
            get { return ciudad; }
            set
            {
                ciudad = value;
                OnPropertyChanged(this);
            }
        }

        private string pais;

        public string Pais
        {
            get { return pais; }
            set
            {
                pais = value;
                OnPropertyChanged(this);
            }
        }

        private bool actualizar;

        public bool Actualizar
        {
            get { return actualizar; }
            set { actualizar = value; }
        }
        
        
        
        public CityWeatherViewModel() {

            configModel();
            LoadSettings();
            updateModel();
            regiserPeriodicTask();
        }

        public void getData() {

            model.getForeCast();
        }

        private void configModel()
        {
            model = new CityWeatherModel();
            model.WeatherResponseReady += (s, e) => {
                Pronosticos = new ObservableCollection<Pronostico>(e.Result);
                updateTile();
            };
        }
        
      

        private void LoadSettings()
        {
            System.IO.IsolatedStorage.IsolatedStorageSettings settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
            if (settings.Contains("Pais"))
            {
                Pais = settings["Pais"].ToString();

            }
            else {

                Pais = "Venezuela";
            }
            if (settings.Contains("Ciudad"))
            {
                Ciudad = settings["Ciudad"].ToString();

            }
            else {
                Ciudad = "Barquisimeto";
            }

            if (settings.Contains("Actualizar"))
            {
                Actualizar = Boolean.Parse(settings["Actualizar"].ToString());
            }
            else
            {
                Actualizar = true;

            }

        }

        private void updateModel()
        {
            model.Ciudad = Ciudad;
            model.Pais = Pais;
        }

        public void UpdateSettings()
        {
            System.IO.IsolatedStorage.IsolatedStorageSettings settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
            if (settings.Contains("Pais"))
            {
                settings["Pais"] = Pais; ;
            }
            else
            {
                settings.Add("Pais", Pais);

            }
            if (settings.Contains("Ciudad"))
            {
                settings["Ciudad"] = Ciudad; ;
            }
            else
            {
                settings.Add("Ciudad", Ciudad);

            }
            if (settings.Contains("Actualizar"))
            {
                settings["Actualizar"] = Actualizar; ;
            }
            else
            {
                settings.Add("Actualizar", Actualizar);

            }

            updateModel();

        }


        private void updateTile()
        {
            var tile = Microsoft.Phone.Shell.ShellTile.ActiveTiles.First();
            var cdata = new Microsoft.Phone.Shell.FlipTileData();
            cdata.Count = int.Parse(pronosticos[0].TempC);
            cdata.BackContent = pronosticos[0].TempC + " C° " + pronosticos[0].TempF + " F°" + Environment.NewLine + Environment.NewLine + pronosticos[0].Condicion; ;
            tile.Update(cdata);
            cdata.BackTitle = "Clima " + Ciudad;
        }


        private void regiserPeriodicTask()
        {
            var taskName = "UpdateWeatherTask";
            PeriodicTask pt = new PeriodicTask(taskName);
            pt.Description = "Actualizar el Clima.";

            var oldtask = ScheduledActionService.Find(taskName);
            if (oldtask != null)
                ScheduledActionService.Remove(taskName);

            if (Actualizar)
            {
                ScheduledActionService.Add(pt);
                ScheduledActionService.LaunchForTest(taskName, new TimeSpan(0, 0, 10));
            }

        }

    }
}
